<template>
  <div :class="className"
       :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from "echarts"

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    headTitle: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "350px"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      required: true
    },
    chartTime: {
      type: Array,
      required: true
    },
    unit: {
      type: String,
      default: ""
    },
    color: {
      type: String,
      default: "#239DD2"
    }
  },
  data() {
    return {
      chart: null,
      chartData1: [],
      chartData2: []
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.handleChartData(val);
        this.setOptions(
          this.headTitle,
          this.chartData,
          this.chartTime,
          this.unit
        );
      }
    },
    chartTime: {
      deep: true,
      handler(val) {
        this.setOptions(
          this.headTitle,
          this.chartData,
          this.chartTime,
          this.unit
        );
      }
    }
  },
  mounted() {
    window.addEventListener("resize", this.resizeHandler);
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeHandler);
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    handleChartData(chartData) {
      this.chartData1 = [];//为0数据
      this.chartData2 = [];//不为0数据
      for (let i = 0; i < chartData.length; i++) {
        let value = chartData[i];
        if (value == 0 || value == null) {
          if (i == 0) {
            this.chartData1.push(0);
            this.chartData1.push(chartData[i + 1]);
            this.chartData2.push('-');
            this.chartData2.push(chartData[i + 1]);

          } else if (i == chartData.length - 1) {
            this.chartData1[i] = 0;
            this.chartData2[i] = '-';
          } else {
            this.chartData1[i] = 0;
            this.chartData1.push(chartData[i + 1]);
            this.chartData2[i] = 0;
            this.chartData2.push(chartData[i + 1]);
          }
        } else {
          if (i == 0) {
            this.chartData2.push(value);
            this.chartData2.push(chartData[i + 1]);
            this.chartData1.push('-');
            this.chartData1.push(chartData[i + 1]);
          } else if (i == chartData.length - 1) {
            this.chartData2[i] = value;
            this.chartData1[i] = '-';
          } else {
            this.chartData2[i] = value;
            this.chartData2.push(chartData[i + 1]);
            this.chartData1[i] = '-';
            this.chartData1.push(chartData[i + 1]);
          }
        }
      }

    },
    resizeHandler() {
      this.chart.resize();
    },
    initChart() {
      this.chart = echarts.init(this.$el);
      this.setOptions(
        this.headTitle,
        this.chartData,
        this.chartTime,
        this.unit
      );
    },
    setOptions(title, chartData, chartTime, unit) {
      var _this = this;
      this.chart.setOption({
        // title: {
        //   text: title,
        //   left: "center"
        // },
        grid: {
          left: 20,
          right: 35,
          bottom: 10,
          top: 35,
          containLabel: true
        },
        xAxis: {
          data: chartTime,
          boundaryGap: false, //falseX轴两边对齐
          //坐标轴刻度相关设置
          axisTick: {
            //是否显示坐标轴刻度
            show: false
          },
        },
        dataZoom: { type: 'inside' },
        yAxis: {
          type: "value",
          // name: "单位(" + unit + ")",
          nameLocation: "end",
          min: function (value) {
            return 0;
            // if (_this.headTitle == "节律刺激因子CS") {
            //   console.log(value.min.toFixed(2));

            //   return value.min.toFixed(2);
            // }
            // if (value.min == "Infinity") {
            //   return 0;
            // }
            // return Math.floor(value.min);
          },
          max: function (value) {
            if (value.max == "-Infinity") {
              return 30;
            }
            if (_this.headTitle == "节律刺激因子CS") {
              return value.max.toFixed(2);
            }
            return Math.ceil(value.max);
          },
          boundaryGap: false,
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: true
          },
          /*axisLabel: {
            formatter: '{value} ' + unit
          }*/
          axisLabel: {
            formatter: function (value) {
              if (_this.headTitle == "节律刺激因子CS") {
                if (value == 0) {
                  return 0;
                }
                return value.toFixed(2);
              }
              return parseInt(value)
            }
          }
        },
        // dataZoom: {
        //   type: 'inside',  //放大缩小x轴数值
        // },
        //提示框组件
        tooltip: {
          //坐标轴触发，主要在柱状图，折线图等会使用类目轴的图表中使用
          trigger: "axis",
          //坐标轴指示器配置项
          axisPointer: {
            //显示X轴线条指示器
            type: "line",//shadow阴影效果
            axis: 'x',
            //坐标轴指示器的文本标签
            label: {
              //文本标签中数值的小数点精度
              precision: 0
            }
          },
          // formatter: '{b0}<br />' + title + '：{c0}' + unit,
          formatter: function (params) {
            for (let i = 0; i < chartTime.length; i++) {
              const element = chartTime[i];
              if (params[0].name == element) {
                if (chartData[i] == null) {
                  return `${element}<br />${title}: NC `
                }

                return `${element}<br />${title}:${chartData[i]}`
              }
            }


          },
          padding: [5, 10]
        },

        series: [
          {
            lineStyle: {
              shadowColor: this.color,
              // shadowOffsetX: 10,
              color: this.color,
              shadowOffsetY: 4,
              opacity: 1,
              shadowBlur: 10,
              width: 4
            },
            smooth: true,
            showSymbol: false,
            name: '测试',
            data: this.chartData2,
            type: 'line',

          }, {
            name: '测试',
            data: this.chartData1,
            type: 'line',
            itemStyle: {
              normal: {
                lineStyle: {
                  shadowColor: 'gray',
                  // shadowOffsetX: 10,
                  color: 'gray',
                  shadowOffsetY: 4,
                  opacity: 1,
                  shadowBlur: 10,
                  width: 4,
                }
              }
            },
            smooth: true,
            showSymbol: false
          }]
      });

    }
  }
};
</script>
